﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ComputerResourceConsole.factory;
using ComputerResourceConsole.lib;
using ComputerResourceConsole.common;
using ComputerResourceConsole.view;
using System.Diagnostics;
using MDL;

namespace ComputerResourceConsole
{
    public partial class frm_console : Form
    {
        private IMongoDBControl _pMgCtrl = null;
        private IRedisControl _pRdsCtrl = null;
        private ComputerResourceConsole.lib.IContainerControl _pCtnCtrl = null;
        private ComputerResourceConsole.lib.IContainerAdvancedControl _pCtnACtrl = null;
        private ISysControl _pSysCtrl = null;
        private frm_about _frm_about = null;
        private frm_debug _frm_debugger = null;
        private frm_version _frm_version = null;
        private frm_packing _frm_packing = null;
        private frm_mdl _frm_mdl = null;

        public frm_console()
        {
            InitializeComponent();

            try
            {
                this._pMgCtrl = ComponentControlFactory.createMongoDBControl();
                this._pRdsCtrl = ComponentControlFactory.createRedisControl();
                this._pCtnCtrl = ComponentControlFactory.createContainerControl();
                this._pSysCtrl = ComponentControlFactory.createSysControl();
                this._pCtnACtrl = ComponentControlFactory.createContainerAdvancedControl();

                ////int mgmark = _pMgCtrl.init(onMongoExit);
                //if (mgmark == 1)
                //{
                //    //this.setMongoView(false, true, true, "Running", Color.Green);
                //}
                //else if (mgmark == -1)
                //{
                //    //this.setMongoView(false, false, false, "Running(Fixed)", Color.Orange);
                //}
                //else
                //{
                //    //this.setMongoView(true, false, false, "Not Running", Color.Red);
                //}

                int ctnmark = _pCtnCtrl.init(onContainerExit);
                if (ctnmark == 1)
                {
                    this.setContainerView(false, true, true, "Running", Color.Green);
                }
                else if (ctnmark == -1)
                {
                    this.setContainerView(false, false, false, "Running(Fixed)", Color.Orange);
                }
                else
                {
                    this.setContainerView(true, false, false, "Not Running", Color.Red);
                }

                this._pSysCtrl.setInboundRulePort("ComputerRes", 8060, crcNetProtocol.TCP);
                if (this._pSysCtrl.IsInboundRulePortExist())
                {
                    this.openFirewallPortToolStripMenuItem.Checked = true;
                }

                this._frm_about = new frm_about();
                this._frm_debugger = new frm_debug();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw ex;
            }
        }

        ////设置MongoDB的界面
        //private void setMongoView(bool startenable, bool stopenable, bool restartenabel, string status, Color txtColor)
        //{
        //    this.bt_mg_start.Enabled = startenable;
        //    this.bt_mg_stop.Enabled = stopenable;
        //    this.l_mg_status.Text = status;
        //    this.l_mg_status.ForeColor = txtColor;
        //}

        //设置Container的界面
        private void setContainerView(bool startenable, bool stopenable, bool restartenabel, string status, Color txtColor)
        {
            this.bt_ctn_start.Enabled = startenable;
            this.bt_ctn_stop.Enabled = stopenable;
            this.l_ctn_status.Text = status;
            this.l_ctn_status.ForeColor = txtColor;
        }

        ////MongoDB退出事件
        //private void onMongoExit(object sender, EventArgs e)
        //{
        //    if (this.bt_mg_start.InvokeRequired)
        //    {
        //        this._pMgCtrl.Status = "Stopped";
        //        CommonMethod.CommonEvent stcb = new CommonMethod.CommonEvent(onMongoExit);
        //        this.Invoke(stcb, new object[] { sender, e });
        //    }
        //    else
        //    {
        //        if (_pMgCtrl.Status == "Stopped")
        //        {
        //            this.setMongoView(true, false, false, "Not Running", Color.Red);
        //        }
        //        else
        //        {
        //            this.setMongoView(false, true, true, "Running", Color.Green);
        //        }
        //    }
        //}

        //Container退出事件
        private void onContainerExit(object sender, EventArgs e)
        {
            if (this.bt_ctn_start.InvokeRequired)
            {
                this._pCtnCtrl.Status = "Stopped";
                CommonMethod.CommonEvent stcb = new CommonMethod.CommonEvent(onContainerExit);
                this.Invoke(stcb, new object[] { sender, e });
            }
            else
            {
                if (this._pCtnCtrl.Status == "Stopped")
                {
                    this.setContainerView(true, false, false, "Not Running", Color.Red);
                }
                else
                {
                    this.setContainerView(false, true, true, "Running", Color.Green);
                }
            }
        }

        //private void bt_mg_start_Click(object sender, EventArgs e)
        //{
        //    this._pMgCtrl.start(onMongoExit);
        //    this.setMongoView(false, true, true, "Running", Color.Green);
        //}

        //private void bt_mg_stop_Click(object sender, EventArgs e)
        //{
        //    this._pMgCtrl.stop();
        //}

        private void bt_rds_stop_Click(object sender, EventArgs e)
        {
            this._pRdsCtrl.stop();
        }

        private void bt_ctn_start_Click(object sender, EventArgs e)
        {
            this._pCtnCtrl.start(onContainerExit);
            this.setContainerView(false, true, true, "Running", Color.Green);
        }

        private void bt_ctn_stop_Click(object sender, EventArgs e)
        {
            this._pCtnCtrl.stop();
        }

        private void openFirewallPortToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._pSysCtrl.AddInboundRulePort();
        }

        private void helpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._pSysCtrl.OpenHelpPage();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._frm_about.ShowDialog();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.notifyIcon1.Visible = false;
            this._pSysCtrl.ApplicationExit();
        }

        private void openDiaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.TopMost = true;
        }

        private void frm_console_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true; 
                this.Hide();
                return;
            }
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.TopMost = true;
        }

        private void openModelSersTableToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._pSysCtrl.OpenVisitorPage();
        }

        private void openManagerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._pSysCtrl.OpenManagerPage();
        }

        private void modelServiceDebuggerToolStripMenuItemToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._frm_debugger = new frm_debug();
            this._frm_debugger.Show();
        }


        private void batchDeploymentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._pCtnACtrl.Arguments = "3";
            this._pCtnACtrl.start(null);
        }

        private void packingToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._frm_packing = new frm_packing();
            this._frm_packing.Show();
        }

        private void mDLGeneratorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._frm_mdl = new frm_mdl();
            this._frm_mdl.Show();
        }

        private void mSDebuggerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._frm_debugger = new frm_debug();
            this._frm_debugger.Show();
        }

        private void mDLGeneratorToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this._frm_mdl = new frm_mdl();
            this._frm_mdl.Show();
        }

        private void packingToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this._frm_packing = new frm_packing();
            this._frm_packing.Show();
        }

        private void updateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._frm_version = new frm_version();
            this._frm_version.Show();
        }

    }
}
